(function() {
    var RAND_MAX = 32767; // 2^16 -1，在二進位是 15 個 1
    var _PI = Math.PI;
    var radius = 3;
    var deltaDist = 3;
    var scaleDist = 10;
    var canvas;
    var ctx;

    var _init = function() {
        canvas = document.getElementById('rnd_demo');
        canvas.width = 2 * canvas.clientWidth + scaleDist;
        canvas.height = 2 * canvas.clientHeight + scaleDist;
	    if (!canvas || !canvas.getContext) {
	    	return;
	    }
	    ctx = canvas.getContext('2d');
        ctx.lineWidth = 3;
    };

    var _canvasLear = function() {
		ctx.clearRect(0, 0, canvas.width, canvas.height);

        ctx.beginPath();
            ctx.moveTo(deltaDist, 0);
            ctx.lineTo(deltaDist, (canvas.height - deltaDist));
            ctx.moveTo(deltaDist, (canvas.height - deltaDist));
            ctx.lineTo(canvas.width, (canvas.height - deltaDist));
        ctx.closePath();
        ctx.stroke();
    };
    var rand_i = function(pts) {
        _canvasLear();

        var _ptsLen = pts[0].length;
        var _w = canvas.width - scaleDist;
        var _h = canvas.height - scaleDist;
        for (var i = 0; i < _ptsLen; i++) {
            var _xi = (pts[0][i] / RAND_MAX) * _w + deltaDist;
            var _yi = ((RAND_MAX - pts[1][i]) / RAND_MAX) * _h - deltaDist;
            ctx.beginPath();
                ctx.arc(_xi, _yi, radius, 0, 2 * _PI, false);
            ctx.closePath();
            ctx.fill();
        }
    };

    var rand_f = function(pts) {
        _canvasLear();

        var _ptsLen = pts[0].length;
        var _w = canvas.width - scaleDist;
        var _h = canvas.height - scaleDist;
        for (var i = 0; i < _ptsLen; i++) {
            var _xi = (pts[0][i]) * _w + deltaDist;
            var _yi = (1 - pts[1][i]) * _h - deltaDist;
            ctx.beginPath();
                ctx.arc(_xi, _yi, radius, 0, 2 * _PI, false);
            ctx.closePath();
            ctx.fill();
        }
    };

    var rand_mm = function(pts, min, max) {
        _canvasLear();

        var _ptsLen = pts.length;
        var _range = Math.abs(max - min);
        var _w = canvas.width - scaleDist;
        var _h = canvas.height - scaleDist;
        for (var i = 0; i < _ptsLen; i++) {
            var _xi = (pts[i].seed) * _w + deltaDist;
            var _yi = ((_range - pts[i].rnd) / _range) * _h - deltaDist;
            ctx.beginPath();
                ctx.arc(_xi, _yi, radius, 0, 2 * _PI, false);
            ctx.closePath();
            ctx.fill();
        };
    };

    var rand_g = function(pts, a, b, c) {
        _canvasLear();

        var a = (!!a) ? (a) : (1 / Math.sqrt(2 * _PI));
        var b = (!!b) ? (b) : (0);
        var c = (!!c) ? (c) : (1);

        var _max = 4 * Math.abs(c);
        var _min = -_max;
        var _range = Math.abs(_max - _min);
        var _ptsLen = pts.length;
        var _w = canvas.width - scaleDist;
        var _h = canvas.height - scaleDist;
        for (var i = 0; i < _ptsLen; i++) {
            var _xi = ((pts[i].seed - _min) / _range) * _w + deltaDist;
            var _yi = ((1.3 * a - pts[i].rnd) / (1.3 * a)) * _h - deltaDist;
            ctx.beginPath();
                ctx.arc(_xi, _yi, radius, 0, 2 * _PI, false);
            ctx.closePath();
            ctx.fill();
        }
    };

    var rand_cos = function(pts, scaleLv, limit, dtheta) {
        _canvasLear();

        var scaleLv = (!!scaleLv) ? (scaleLv) : (1);
        var limit = (!!limit) ? (limit) : (1);
        var dtheta = (!!dtheta) ? (dtheta) : (0);
        var _max = scaleLv * _PI + dtheta;
        var _min = dtheta;
        var _range = Math.abs(_max - _min);
        var _ptsLen = pts.length;
        var _w = canvas.width - scaleDist;
        var _h = canvas.height - scaleDist;
        for (var i = 0; i < _ptsLen; i++) {
            var _xi = ((pts[i].seed - _min) / _range) * _w + deltaDist;
            var _yi = ((1.3 * limit - pts[i].rnd) / (2.6 * limit)) * _h - deltaDist;
            ctx.beginPath();
                ctx.arc(_xi, _yi, radius, 0, 2 * _PI, false);
            ctx.closePath();
            ctx.fill();
        }
    };

    var rand_boxmuller = function(pts) {
        _canvasLear();

        var _max =  3;
        var _min = -3;
        var _range = Math.abs(_max - _min);
        var _ptsLen = pts.length;
        var _w = canvas.width - scaleDist;
        var _h = canvas.height - scaleDist;
        var r = 1.5;
        for (var i = 0; i < _ptsLen; i++) {
            var _xi = ((pts[i].rnd - _min) / _range) * _w + deltaDist;
            var _yi = ((r - pts[i].seed) / (r)) * _h - deltaDist;
            ctx.beginPath();
                ctx.arc(_xi, _yi, radius, 0, 2 * _PI, false);
            ctx.closePath();
            ctx.fill();
        }
    };

    var _present = {
        '_init': _init,
        'rand_i': rand_i,
        'rand_f': rand_f,
        'rand_mm': rand_mm,
        'rand_g': rand_g,
        'rand_cos': rand_cos,
        'rand_boxmuller': rand_boxmuller
    };
    window.PRESENT = _present;
})();
